package com.norns.torque.kvwrapper

import android.text.TextUtils
import android.util.Base64
import java.io.*

object SpUtil {

    fun saveInt(key: String, value: Int) {
        SpKV.instance.putInt(key,value)
    }

    fun getInt(key: String): Int {
        return SpKV.instance.getInt(key,-1)
    }

    fun getInt(key: String, defValue: Int): Int {
        return SpKV.instance.getInt(key,defValue)
    }

    fun saveBoolean(key: String, value: Boolean) {
        SpKV.instance.putBoolean(key,value)
    }

    fun getBoolean(key: String): Boolean {
        return SpKV.instance.getBoolean(key, false)
    }

    fun getBoolean(key: String, isTrue: Boolean): Boolean {
        return SpKV.instance.getBoolean(key, isTrue)
    }

    fun saveLong(key: String, value: Long) {
        SpKV.instance.putLong(key,value)
    }

    fun getLong(key: String, defValue: Long): Long {
        return SpKV.instance.getLong(key, defValue)
    }

    fun getLong(key: String): Long {
        return SpKV.instance.getLong(key, -1)
    }

    fun saveFloat(key: String, value: Float) {
        SpKV.instance.putFloat(key,value)
    }

    fun getFloat(key: String): Float {
        return SpKV.instance.getFloat(key, -1f)
    }
    fun getFloat(key: String, defValue: Float): Float {
        return SpKV.instance.getFloat(key, defValue)
    }

    fun saveString(key: String, value: String?) {
        if (value.isNullOrEmpty()) {
            SpKV.instance.putString(key, "")
        }else{
            SpKV.instance.putString(key, value)
        }
    }

    fun getString(key: String): String {
        return SpKV.instance.getString(key, "")
    }

    fun getString(key: String, defValue: String): String {
        return SpKV.instance.getString(key, defValue)
    }


    fun remove(key: String) {
        SpKV.instance.remove(key)
    }

    /**
     * 保存序列化过的对象
     *
     * @param key
     * @param obj
     */
    fun saveObj(key: String, obj: Any?) {
        if (obj == null) return
        require(obj is Serializable) { "The object should implements Serializable!" }
        //1.write obj to bytes
        val baos = ByteArrayOutputStream()
        try {
            val oos = ObjectOutputStream(baos)
            oos.writeObject(obj)

            //2.convert obj to string via Base64
            val bytes = Base64.encode(baos.toByteArray(), Base64.DEFAULT)

            //3.save string
            saveString(key, String(bytes))
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    /**
     * 读取序列化过的对象
     *
     * @param key
     * @return
     */
    fun getObj(key: String): Any? {
        //1.get string
        val string = SpKV.instance.getString(key, "")
        if (TextUtils.isEmpty(string)) return null
        //2.decode string
        val bytes = Base64.decode(string, Base64.DEFAULT)
        //3.read bytes to Object
        val bais = ByteArrayInputStream(bytes)
        try {
            val ois = ObjectInputStream(bais)
            return ois.readObject()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }
}